package com.iampzh.oa.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.iampzh.oa.dao.PostDaoImpl;
import com.iampzh.oa.entitys.Post;

@Controller
@RequestMapping("post")
public class PostController {
	@Autowired
	private PostDaoImpl postImpl;

	/**
	 * 显示所有岗位信息
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("list")
	public String postList(Model model) {
		List<Post> allPost = postImpl.getAllPost();
		model.addAttribute("postlist", allPost);
		return "/post/list";

	}

	/**
	 * 跳转到修改岗位页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("roleAction_editUI/{id}")
	public String editUi(@PathVariable("id") long id, Model model) {
		Post post = postImpl.getPost((int) id);
		model.addAttribute("post", post);
		return "/post/editUI";
	}

	/**
	 * 修改岗位操作
	 * 
	 * @param request
	 * @return
	 */
	@PostMapping("roleAction_editUI/roleAction_edit")
	public String updatePost(HttpServletRequest request) {
		String pid = request.getParameter("id");
		String pname = request.getParameter("newname");
		String pdesc = request.getParameter("newdesc");
		Post newPost = new Post();
		newPost.setPid(Long.parseLong(pid));
		newPost.setPdesc(pdesc);
		newPost.setPname(pname);
		postImpl.save(newPost);
		return "redirect:../list";

	}

	/**
	 * 跳转到新增岗位页面
	 * 
	 * @return
	 */
	@RequestMapping("roleAction_addUI")
	public String showAdd() {
		return "post/addUI";
	}

	@PostMapping("roleAction_add")
	public String addPost(HttpServletRequest request) {
		String pname = request.getParameter("pname");
		String pdesc = request.getParameter("pdesc");
		Post post = new Post();
		post.setPdesc(pdesc);
		post.setPname(pname);
		postImpl.save(post);
		return "redirect:list";
	}

	@RequestMapping("roleAction_delete/{id}")
	public String deletePost(@PathVariable("id") int id) {
		postImpl.delete( id);
		return "redirect:../list";
	}
}
